package com.tim.curl;

import lombok.Data;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Data
public class FetchObject {

    private String url;

    private Map<String, String> headers;

    private String referrer;

    private String referrerPolicy;

    private String body;

    private String method;

    private String mode;

    private String credentials;

    public RequestBody getRequestBody(){
        RequestBody requestBody=null;
        String body = getBody();
        if(body!=null){
            requestBody=RequestBody.create(MediaType.get("application/json"), body);
        }
        return requestBody;
    }

    public Map<String, String> getHeadersMap(){
        Map<String, String> map=new HashMap<>(headers);
        if(StringUtils.isNotBlank(referrer)){
            map.put("referrer", referrer);
        }
        return map;
    }
}
